package algorithms.que12;

import edu.princeton.cs.algs4.Interval1D;
import edu.princeton.cs.algs4.Interval2D;
import edu.princeton.cs.algs4.StdRandom;

import java.util.ArrayList;
import java.util.List;


/**
 * @功能
 * @创建时间 2020/6/21
 */
public class Interval2D3 {
    public static void main(String[] args) {
        draw(20,.1,.5);
    }
    public static void draw(Integer N,double min,double max){
        List<Interval2D> ww =new ArrayList<>();
        List<Double> xl = new ArrayList<>();
        List<Double> xh = new ArrayList<>();
        List<Double> yl = new ArrayList<>();
        List<Double> yh = new ArrayList<>();
        for(int i=0;i<N;i++){
            double xlo= StdRandom.uniform(min,max);
            double xhi=StdRandom.uniform(min,max);
            while (xlo>=xhi){
                 xhi=StdRandom.uniform(min,max);
            }
            xl.add(xlo);
            xh.add(xhi);
            double ylo= StdRandom.uniform(min,max);
            double yhi=StdRandom.uniform(min,max);
            while ( ylo>=yhi){
                yhi=StdRandom.uniform(min,max);
            }
            yl.add(ylo);
            yh.add(yhi);
            Interval1D a = new Interval1D(xlo,xhi);
            Interval1D b = new Interval1D(ylo,yhi);
            Interval2D c = new Interval2D(a,b);
            c.draw();
            ww.add(c);
        }
        compare(ww);
        contain(xl,xh,yl,yh);
    }
    public static void compare(List<Interval2D> w){
        int count=0;
        int i=0;
        while (i!=w.size()-1){
            for (int j=i;j<w.size()-1;j++){
                if(w.get(i).intersects(w.get(j+1))){
                    //              System.out.println(count);
                    count++;
                }
            }
            i++;

        }
        System.out.println(count);
    }
    public static void contain(List<Double> xl , List<Double> xh , List<Double> yl , List<Double> yh ){
        int count=0;
        int i=0;
       while (i!=xl.size()-1){
           for (int j=i;j<xl.size()-1;j++){
              if((xl.get(j)<xl.get(j+1)&&xh.get(j)>xh.get(j+1)&&
                      yl.get(j)<yl.get(j+1)&&yh.get(j)>yh.get(j+1))||
                      (xl.get(j)>xl.get(j+1)&&xh.get(j)<xh.get(j+1)&&
                              yl.get(j)>yl.get(j+1)&&yh.get(j)<yh.get(j+1))){
                  count++;
              }
           }
           i++;
       }

        System.out.println(count);
    }



    }

